local PartnerUseExpPanel = app.mvc.createPanelClass(...)
local PartnerUseExpRender = import(".renders.PartnerUseExpRender")
--data =
function PartnerUseExpPanel.createWithData(data, node)
    local sprite = PartnerUseExpPanel.extend(node or cc.CSLoader:createNode(Res.PartnerUseExpPanel ))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)

    return sprite
end


function PartnerUseExpPanel.create(node)
    return PartnerUseExpPanel.createWithData(nil, node)
end


function PartnerUseExpPanel:init(data)
    self.super.init(self, data)
    local dataStr = GD:getSystemStrPar( "partnerExpItem", "")
    local idsArr = string.split(dataStr, ";")

    self._items = {}
    for k, v in pairs(idsArr) do
        local item = PartnerUseExpRender.create(nil, {id = tonumber(v), type = 6})
        self._ui["Panel/ListView"]:pushBackCustomItem(item)
        table.insert(self._items, item)
    end
    -- self._ui["Panel"]:setAnchorPoint(cc.p(0,0))
    display.nodeRemoveForNotHit( self._ui["Panel"], function()
        self:setVisible(false)
    end ) 
    self._ui["Panel/exp/Slider"]:setTouchEnabled(false)
    self._actionEnd = true  

    self._levelUpAniState = 0

    self._timeLine = cc.CSLoader:createTimeline(Res.PartnerUseExpAnimation)
    self._ui["Panel/ani"]:stopAllActions()
    self._ui["Panel/ani"]:runAction(self._timeLine)
    self._ui["Panel/ani"]:setVisible(true)
    self._timeLine:gotoFrameAndPause(0)
   
    -- self._timeLine:setLastFrameCallFunc(function()
    --     if self._levelUpAniState == 2 then
    --         self._rolePropAniState = 0
    --         self._ui["Panel/Panel_role/top"]:setVisible( false )
    --         self._rolePorpTouchLayer:setVisible(false)
    --     end
    -- end)
end

function PartnerUseExpPanel:playEffectLevelUp()
    if self._timeLine then
        self._levelUpAniState = 1
        self._ui["Panel/ani/Panel/flash_01"]:resetSystem()
        self._timeLine:play( "animation", false )
    end
end

-- function PartnerUseExpPanel:stopEffectLevelUp()
--     self._timeLine:gotoAndPause(0)
--     self._levelUpAniState = 0
-- end


function PartnerUseExpPanel:refresh()
    if not self._data then
        return
    end
    if self._actionEnd then
        local data = self._data
        local lv = data:getLevel()
        local maxExp = GD:queryPartnetLevelUpExpConfig(lv)
        local curExp = data:getExp()
        self:updateBar(curExp, maxExp, data:getLevel())
    end
end

function PartnerUseExpPanel:updateBarByActionState(curExp, maxExp, curLv, state, isLevelUp)
    if state == "End" then
        self._actionEnd = true
        self:refresh()
    elseif state == "Start" then
        self._actionEnd = false
    else
        self:updateBar(curExp, maxExp, curLv, isLevelUp)
    end
end

function PartnerUseExpPanel:updateBar(curExp, maxExp, curLv, isLevelUp)
    local percent = curExp * 100 /maxExp
    if percent > 100 then
        percent = 100
    end
    self._ui["Panel/exp/num"]:setString(string.format("%s/%s", math.floor(curExp), maxExp))
    self._ui["Panel/exp/Slider"]:setPercent(percent)
    self._ui["Panel/exp/lv"]:setString(string.format(L("lua_code_text_212"), curLv or 0))
    if isLevelUp then
        self:playEffectLevelUp()
    end
end


function PartnerUseExpPanel:updatePartnerData(p)
    for k, v in pairs(self._items) do
        v:updatePartnerData(p)
    end
end


function PartnerUseExpPanel:stopRenderActions()
    for k, v in pairs(self._items) do
        v:stopRenderAction()
    end
    self._actionEnd = true
end


function PartnerUseExpPanel:setRenderParentState(trueOrFasle)
    for k, v in pairs(self._items) do
        v:setParentVisible(trueOrFasle)
    end
end


return PartnerUseExpPanel